package com.cs171.market.controller;

/**
 * @author Endy
 * @create 2020 - 05 - 21 - 19:17
 * @date 2020/5/21 - 19:17
 *
 * (classify)页面控制层
 *
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cs171.market.Repository.GoodRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
public class ClassifyController {

    /**
     * 服务对象
     */
    @Autowired
    private GoodRepository repository;

    @GetMapping("/classify")
    public Object classifyJson(){


        JSONArray classifyRecommend = (JSONArray) JSON.toJSON(repository.recommendnameJsonAboutGoods("bestSelling"));
        JSONObject classifyRecommendJson = new JSONObject();
        classifyRecommendJson.put("id", 1);
        classifyRecommendJson.put("category", "热门推荐");
        classifyRecommendJson.put("foodlist", classifyRecommend);

        JSONArray classifyFruit = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("水果"));
        JSONObject classifyFruitJson = new JSONObject();
        classifyFruitJson.put("id", 2);
        classifyFruitJson.put("category", "美味水果");
        classifyFruitJson.put("foodlist", classifyFruit);

        JSONArray classifyVegetable = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("蔬菜"));
        JSONObject classifyVegetableJson = new JSONObject();
        classifyVegetableJson.put("id", 3);
        classifyVegetableJson.put("category", "新鲜蔬菜");
        classifyVegetableJson.put("foodlist", classifyVegetable);

        JSONArray classifyMeat = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("肉类"));
        JSONObject classifyMeatJson = new JSONObject();
        classifyMeatJson.put("id", 4);
        classifyMeatJson.put("category", "精选肉类");
        classifyMeatJson.put("foodlist", classifyMeat);

        JSONArray classifySeafood = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("海鲜"));
        JSONObject classifySeafoodJson = new JSONObject();
        classifySeafoodJson.put("id", 5);
        classifySeafoodJson.put("category", "海鲜水产");
        classifySeafoodJson.put("foodlist", classifySeafood);

        JSONArray classifyDrink = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("饮品"));
        JSONObject classifyDrinkJson = new JSONObject();
        classifyDrinkJson.put("id", 6);
        classifyDrinkJson.put("category", "乳品饮品");
        classifyDrinkJson.put("foodlist", classifyDrink);

        JSONArray classifyFastfood = (JSONArray)JSON.toJSON(repository.getClassifynameJsonAboutGoods("速食"));
        JSONObject classifyFastfoodJson = new JSONObject();
        classifyFastfoodJson.put("id", 7);
        classifyFastfoodJson.put("category", "方便速食");
        classifyFastfoodJson.put("foodlist", classifyFastfood);

        JSONArray classifyArry = new JSONArray();
        classifyArry.add(classifyRecommendJson);
        classifyArry.add(classifyFruitJson);
        classifyArry.add(classifyVegetableJson);
        classifyArry.add(classifyMeatJson);
        classifyArry.add(classifySeafoodJson);
        classifyArry.add(classifyDrinkJson);
        classifyArry.add(classifyFastfoodJson);

        JSONObject dataJson = new JSONObject();
        dataJson.put("cateList",classifyArry);

        JSONObject classifyJson = new JSONObject();
        classifyJson.put("success","true");
        classifyJson.put("data",dataJson);

        return classifyJson;

    }
}
